#include "stm32f10x.h"
#include "freertos.h"
#include "task.h"
#include "UART1.h"
#include "UART2.h"
#include <string.h>
QueueHandle_t queue;

void task1(void * arg) {
    char buf[30] = {0};
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct);
    GPIO_ResetBits(GPIOA, GPIO_Pin_0);
	
    while (1) {
        char ch;
        BaseType_t ret = xQueueReceive(queue, &ch, 1000);

        // printf1("task \r\n");

        if (ret == pdFAIL) {
            // 没收到数据:
        } else {
            // 收到数据

            buf[strlen(buf)] = ch;

            if (strlen(buf) ==16) {
				printf2("%s\r\n",buf);
                GPIO_SetBits(GPIOA, GPIO_Pin_0);
                char buf1[5] = {0};
                char buf2[10] = {0};
                for (int i = 0; i < 4; i++) {
                    buf1[i] = buf[i+1];
                }

                for (int i = 0; i < 9; i++) {
                    buf2[i] = buf[i + 6];
                }
				printf2("AT+CWJAP_DEF=\"%s\",\"%s\"", buf1, buf2);

            }
        }
    }

}

int main(void) {
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
    USART1_Init();
    USART2_Init();

    queue = xQueueCreate(100, 1);

    xTaskCreate(task1, "Task1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
    vTaskStartScheduler();
}

